﻿using SharPy.GameObjects;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace SharPy.Parser
{
    static class clParser
    {
        /// <summary>
        /// The procedure to merge command lines into one
        /// </summary>
        /// <param name="pText"></param>
        /// <returns></returns>
        internal static List<clTextLine> lineMerge(List<clTextLine> pText)
        {
            List<clTextLine> toReturn = pText;
            for (int i = toReturn.Count - 1; i > 0; i--)
            {
                if (Regex.IsMatch(toReturn[i].sLine, "^with dissolve", RegexOptions.IgnoreCase)
                    || Regex.IsMatch(toReturn[i].sLine, "^with fade", RegexOptions.IgnoreCase))
                {
                    toReturn[i - 1].sLine = toReturn[i - 1].sLine + " " + toReturn[i].sLine;
                }
            }

            toReturn.RemoveAll(l => Regex.IsMatch(l.sLine, "^with dissolve", RegexOptions.IgnoreCase));
            toReturn.RemoveAll(l => Regex.IsMatch(l.sLine, "^with fade", RegexOptions.IgnoreCase));

            foreach (clTextLine l in toReturn)
            {
                l.nLineNo = toReturn.IndexOf(l);
            }
            return toReturn;
        }

        /// <summary>
        /// Regular expression matching one of patterns
        /// </summary>
        /// <param name="sCurLine"> line to match </param>
        /// <returns>Returns a command instance to game screen (GamePage.xaml)</returns>
        internal static clCommand lineMatch(string sCurLine)
        {
            //clCommand objCom = new clCommand();
            //Deleting special symbols
            sCurLine = sCurLine.Replace("\t", "");

            //Matching patterns
            if (Regex.IsMatch(sCurLine, "^#.*"))
                return (new clCommand(clCommand.Name.Comment));

            //Python scripts
            if (Regex.IsMatch(sCurLine, @"\$.*"))
                return (new clCommand(clCommand.Name.Comment));

            //Game commands
            if (Regex.IsMatch(sCurLine, "^define"))
            {
                Regex patternName = new Regex(@"'(\w*)'", RegexOptions.IgnoreCase);
                Regex patternColor = new Regex(@"color=.*(?<match>#\w*)", RegexOptions.IgnoreCase);
                Regex patternLabel = new Regex(@"define\s+(?<match>\w*)\s", RegexOptions.IgnoreCase);

                Match matchName = patternName.Match(sCurLine);
                Match matchLabel = patternLabel.Match(sCurLine);
                Match matchColor = patternColor.Match(sCurLine);

                string sName = matchName.Value;
                string sLabel = matchLabel.Groups["match"].Value;
                string sColor = matchColor.Groups["match"].Value;

                SharPy.GameClasses.clCharacters.addCharacter(sLabel, sName, sColor);

                return new clCommand();
            }

            else if (Regex.IsMatch(sCurLine, "^image.*"))
            {
                MatchCollection m = Regex.Matches(sCurLine, @"(image)(.*?)(=)(.*)");
                SharPy.GameClasses.clImages.addImage(m[0].Groups[2].Value.Trim(),
                                                     m[0].Groups[4].Value.Trim());
                return (new clCommand());
            }

            else if (Parser.clCommands.SetBackground.RegExpCheck(sCurLine))
                return Parser.clCommands.SetBackground.Parse(sCurLine);

            else if (clMusic.RegExpCheck(sCurLine))
                return clMusic.Parse(sCurLine).Result;

            else if (Parser.clCommands.ShowMenu.RegExpCheck(sCurLine))
                return Parser.clCommands.ShowMenu.Parse(sCurLine);

            else if (Parser.clCommands.ShowImage.RegExpCheck(sCurLine))
                return Parser.clCommands.ShowImage.Parse(sCurLine);

            else if (Parser.clCommands.HideImage.RegExpCheck(sCurLine))
                return Parser.clCommands.HideImage.Parse(sCurLine);

            else if (Regex.IsMatch(sCurLine, "^label.*"))
                return new clCommand();

            else if (Regex.IsMatch(sCurLine, "^jump.*"))
                return (new clCommand(clCommand.Name.JumpToLabel
                                        , Regex.Replace(sCurLine, @"jump", "").Trim()
                        ));

            else if (Regex.IsMatch(sCurLine, "^return.*"))
                return new clCommand(clCommand.Name.EndGame);

            //Screen shake
            else if (Regex.IsMatch(sCurLine, "^with vpunch"))
                return new clCommand(clCommand.Name.ScreenAnimation, "verticalPunch");
            else if (Regex.IsMatch(sCurLine, "^with hpunch"))
                return new clCommand(clCommand.Name.ScreenAnimation, "horizontalPunch");

            else if (Regex.IsMatch(sCurLine, "^pause.*"))
                return new clCommand(clCommand.Name.DoNothing);

            //Temporaryly blocked until python variables implemented
            else if (Regex.IsMatch(sCurLine, "^if.*"))
                return new clCommand(clCommand.Name.DoNothing);

            //Showing text
            else if (Parser.clCommands.ShowText.RegExpCheck(sCurLine))
            {
                return Parser.clCommands.ShowText.Parse(sCurLine);
            }

            else
            {
                sCurLine = clText.cleanString(sCurLine);
                return new clCommand(clCommand.Name.ShowText
                        , sCurLine.Trim('\"')
                        );
            }
        }
    }
}